const host ='http://localhost:5000'

function btn_add(){
    console.log('新增');
    location.href='./AddOrEdit.html'
}


function btn_filter(){
    console.log('查询');
    
    let keyword =$('[name=keyword]').val()
    axios.get(`${host}/blogs?keyword=${keyword}`).then((res)=>{
        $('tr[class^=blog]').remove()

        res.data.data.forEach(item=>{
            let html =`
            <tr class="blog${item.id}">
                <td>${item.id}</td>
                <td>${item.title}</td>
                <td>${item.author}</td>
                <td>
                    <input type="button" value="编辑" onclick="btn_Edit()">
                    <input type="button" value="删除" onclick="btn_Del()">
                </td>
            </tr>
        `
        $('tbody').append(html)
        })
    })
}


function btn_Edit(id){
    console.log('编辑');

    
}


function btn_Del(id){
    console.log('删除');
    if (confirm('确认删除吗？')) {
        axios.delete(`${host}/blogs/${id}`).then((res)=>{
            if (res.data.code === 1000) {
                location.href='./index.html'
            }
        })
    }
}

async function btn_Save(){
    console.log('保存');

    let id = new URLSearchParams(location.search).get('id')||0;
    const title= $('[name=title]').val()
    const author =$('[name=author]').val()

    let res
    if (id) {
        res=await axios.put(`${host}/blogs/${id}`,{title,author})
    }else{
        res=await axios.post(`${host}/blogs`,{title,author})
    }

    if (res.data.code === 1000) {
        location.href='./index.html'
    }
    
}

function btn_Cancel(){
    console.log('取消');
    location.href='./index.html'
    
}
